var itemAllotPageVm  = new Vue({
    el: '#itemAllotPage',
    data: {
        /* 报价商品显示 */
        quoteItemAllotPageVisible: false,
        /* 标品显示(分配至自营门店) */
        itemAllotPageVisible: false,
        /* 标品显示(分配至第三方) */
        quoteStandardItemAllotPageVisible: false,
        itemAllotloading: false,
        sellerId: '',
        orderNo: '',
        erpItemId: '',
        itemId: '',
        itemName: '',
        sellerType: '',// 分配供应商类型 1：自营门店 2：第三方
        allotType: '',// 分配商品类型 1：报价  2：标品
        factoryQuoteInfos: [],// 原报价工厂列表
        factoryQuoteInfosQuery: [],// 过滤后报价工厂列表
        factoryStandardInfos: [],
        factoryStandardInfosQuery: [],
        factoryInfosQuery: [],
        q: {
            factoryName: '',
            factoryAddr: ''
        },
    },
    methods: {
        loadData: function (data) {
            itemAllotPageVm.sellerId = data.sellerId;
            itemAllotPageVm.orderNo = data.orderNo;
            itemAllotPageVm.erpItemId = data.erpItemId;
            itemAllotPageVm.itemId = data.itemId;
            itemAllotPageVm.itemName = data.itemName;
            itemAllotPageVm.allotType = data.allotType;
            itemAllotPageVm.sellerType = data.sellerType;
            if (itemAllotPageVm.allotType == 1){
                // 报价商品分配
                $.get(baseURL + "order/erporder/quoteOrderItemAllotFindFactory?itemId=" + data.itemId + "&sellerType=" + data.sellerType, function(r){
                    vm.allotLoading = false;
                    if (r.code == 0){
                        if (itemAllotPageVm.sellerType == 1){// 分配至自营门店
                            itemAllotPageVm.factoryInfosQuery = r.factoryInfosQuery;
                            itemAllotPageVm.itemAllotPageVisible = true;
                        }else { // 分配至第三方
                            itemAllotPageVm.factoryQuoteInfos = r.factoryQuoteInfos;
                            itemAllotPageVm.factoryQuoteInfosQuery = r.factoryQuoteInfos;
                            itemAllotPageVm.quoteItemAllotPageVisible = true;
                        }
                    }else{
                        alert(r.msg);
                    }
                });
            }else{
                // 标品分配
                $.get(baseURL + "order/erporder/orderItemAllotFindFactory?itemId=" + data.itemId + "&sellerType=" + data.sellerType, function(r){
                    vm.allotLoading = false;
                    if (r.code == 0){
                        if (itemAllotPageVm.sellerType == 1){// 分配至自营门店
                            itemAllotPageVm.factoryInfosQuery = r.factoryInfosQuery;
                            itemAllotPageVm.itemAllotPageVisible = true;
                        }else {// 分配至第三方
                            itemAllotPageVm.factoryStandardInfos = r.factoryStandardInfosQuery;
                            itemAllotPageVm.factoryStandardInfosQuery = r.factoryStandardInfosQuery;
                            itemAllotPageVm.quoteStandardItemAllotPageVisible = true;
                        }
                    }else{
                        alert(r.msg);
                    }
                });
            }
        },
        queryFactoryQuote: function () {
            var factoryName = itemAllotPageVm.q.factoryName;
            var factoryAddr = itemAllotPageVm.q.factoryAddr;
            if (isBlank(factoryName) && isBlank(factoryAddr)){
                itemAllotPageVm.factoryQuoteInfosQuery = itemAllotPageVm.factoryQuoteInfos;
            }else{
                itemAllotPageVm.factoryQuoteInfosQuery = [];
                for (var f in itemAllotPageVm.factoryQuoteInfos){
                    var queryNameHit = false;
                    var queryAddrHit = false;
                    var item = itemAllotPageVm.factoryQuoteInfos[f];
                    if (!isBlank(factoryName)){
                        if (item.factoryInfoVO.factoryName.indexOf(factoryName) != -1){
                            queryNameHit = true;
                        }
                    }else {
                        queryNameHit = true;
                    }
                    if (!isBlank(factoryAddr)){
                        if (item.factoryInfoVO.address.indexOf(factoryAddr) != -1){
                            queryAddrHit = true;
                        }
                    }else{
                        queryAddrHit = true;
                    }
                    if (queryNameHit && queryAddrHit){
                        itemAllotPageVm.factoryQuoteInfosQuery.push(item);
                    }
                }
            }
        },
        queryFactoryStandard: function () {
            var factoryName = itemAllotPageVm.q.factoryName;
            if (isBlank(factoryName)){
                itemAllotPageVm.factoryStandardInfosQuery = itemAllotPageVm.factoryStandardInfos;
            }else{
                itemAllotPageVm.factoryStandardInfosQuery = [];
                for (var f in itemAllotPageVm.factoryStandardInfos){
                    var queryNameHit = false;
                    var item = itemAllotPageVm.factoryStandardInfos[f];
                    if (!isBlank(factoryName)){
                        if (item.sellerDetail.merchantName.indexOf(factoryName) != -1){
                            queryNameHit = true;
                        }
                    }else {
                        queryNameHit = true;
                    }
                    if (queryNameHit){
                        itemAllotPageVm.factoryStandardInfosQuery.push(item);
                    }
                }
            }
        },
        /* 标品分配 */
        itemAllotSave: function (row) {
            var merchantName, merchantId, totalPrice, self, price, csRemark;
            if (itemAllotPageVm.sellerType == 1){// 分配至自营门店
                factoryName = row.merchantName;
                producer = row.merchantId;
                cost = row.totalPrice;
                price = row.price;
                self = row.self;
                csRemark = row.csRemark;
            }else {//分配至第三方
                factoryName = row.sellerDetail.merchantName;
                producer = row.sellerDetail.merchantId;
                self = row.sellerDetail.isSelf;
                cost = row.basePriceDetail.totalPrice;
                csRemark = row.csRemark;
            }

            this.$confirm('确认分配至 '+ factoryName +' ?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        var postData = {
                            orderNo: itemAllotPageVm.orderNo,
                            erpItemId: itemAllotPageVm.erpItemId,
                            itemId: itemAllotPageVm.itemId,
                            producer: producer,
                            cost: cost,
                            price: price,
                            self: self,
                            csRemark: csRemark,
                            csId: vm.erpOrder.csId,
                            pmcId: vm.erpOrder.pmcId,
                            sellerType: itemAllotPageVm.sellerType
                        };
                        $.ajax({
                            type: "POST",
                            url: baseURL + "order/erporder/allotOrderItem",
                            contentType: "application/json",
                            data: JSON.stringify(postData),
                            success: function(r){
                                if(r.code == 0){
                                    itemAllotPageVm.itemAllotPageVisible = false;
                                    itemAllotPageVm.quoteStandardItemAllotPageVisible = false;
                                    vm.getInfo(itemAllotPageVm.orderNo);
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        /* 报价商品分配 */
        quoteItemAllotSave: function (row) {
            var factoryName = row.factoryInfoVO.factoryName;
            var id = row.factoryInfoVO.id;
            var totalPrice = row.totalPrice;
            var self = row.factoryInfoVO.self;
            var csRemark = row.csRemark;
            this.$confirm('确认分配至 '+ factoryName +' ?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        var postData = {
                            orderNo: itemAllotPageVm.orderNo,
                            erpItemId: itemAllotPageVm.erpItemId,
                            itemId: itemAllotPageVm.itemId,
                            producer: id,
                            cost: totalPrice,
                            self: self,
                            csRemark: csRemark,
                            csId: vm.erpOrder.csId,
                            pmcId: vm.erpOrder.pmcId,
                            sellerType: itemAllotPageVm.sellerType
                        };
                        $.ajax({
                            type: "POST",
                            url: baseURL + "order/erporder/allotOrderItem",
                            contentType: "application/json",
                            data: JSON.stringify(postData),
                            success: function(r){
                                if(r.code == 0){
                                    itemAllotPageVm.quoteItemAllotPageVisible = false;
                                    vm.getInfo(itemAllotPageVm.orderNo);
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        closeItemAllotPage: function () {

        },
    }
})